package controller;

import config.Config;
import javafx.event.*;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 * 登录界面的controller
 */
public class Login {
    /**
     * 数据库连接对象
     */
    private Connection conn = null;
    /**
     * sql语句对象
     */
    private Statement stmt = null;
    /**
     * 返回结果集
     */
    private ResultSet res = null;
    /**
     * 数据库执行语句
     */
    private String sql = null;
    /**
     * 账户类型选项框对象
     */
    @FXML
    private ComboBox loginType;
    /**
     * 账户输入框对象
     */
    @FXML
    private TextField loginAccount;
    /**
     * 密码输入框对象
     */
    @FXML
    private TextField loginPass;
    /**
     * 登录按钮对象
     */
    @FXML
    private Button loginBtn;
    /**
     * 退出按钮对象
     */
    @FXML
    private Button exitBtn;

    /**
     * 初始化选项框
     */
    @FXML
    public void initialize() {
        loginType.getItems().addAll("患者","医生");
        loginType.getSelectionModel().select(0);
    }

    /**
     * 用户登录触发函数
     */
    @FXML
    public void loginBtnClicked() {
        try {
            conn = DriverManager.getConnection(Config.DB_URL, Config.USER, Config.PASS);
            stmt = conn.createStatement();
            String num = null;
            int type = 0;
            //判断时患者还是医生
            if(loginType.getValue().equals("患者")) {
                sql = "select *" + " from " + Config.patientTable + " where " + Config.patientName + " = ?";
                num = Config.patientNum;
                type = 0;
            }
            else {
                sql = "select *" + " from " + Config.doctorTable + " where " + Config.doctorName + " = ?";
                num = Config.doctorNum;
                type = 1;
            }
            //查询对应账号的密码
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setString(1, loginAccount.getText());
            System.out.println(sql);
            res = pstmt.executeQuery();
            //获取用户密码
            String password = null;
            while (res.next()) {
                password = res.getString(Config.patientPass);
                Main.accountNum = res.getString(num);
                System.out.println(password);
                System.out.println(Main.accountNum);
                System.out.println(res.getString("last_login"));
            }
            //密码为空，用户不存在
            if(password == null) {
                JOptionPane.showMessageDialog(null, "账号不存在", "登录", JOptionPane.WARNING_MESSAGE);
            }
            else if(password.equals(loginPass.getText())) {
                System.out.println("successful");
                JOptionPane.showMessageDialog(null, "登录成功", "登录", JOptionPane.PLAIN_MESSAGE);
                SimpleDateFormat loginDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if(loginType.getValue().equals("患者")) {
                    sql = "update patient set "+ Config.patientLogin +" = "
                            + "'"+loginDate.format(new Date())+ "' where " + Config.patientNum +  " = '" + Main.accountNum + "'";
                }
                else
                {
                    sql = "update doctor set "+ Config.doctorLogin +" = "
                            + "'"+loginDate.format(new Date())+ "' where " + Config.doctorNum +  " = '" + Main.accountNum + "'";
                }
                //更新登录时间
                System.out.println(sql);
                stmt = conn.createStatement();
                stmt.executeUpdate(sql);
                //根据用户类型切换到不同的scene
                if(type == 0)
                {
                    Main.setRegister();
                }else {
                    System.out.println("doctor");
                    Main.setDoctor();
                }
            }
            else {
                //密码错误
                JOptionPane.showMessageDialog(null, "密码错误", "登录", JOptionPane.WARNING_MESSAGE);
            }
            conn.close();
            return;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 退出按钮触发函数
     * @param event
     * @throws SQLException
     */
    @FXML
    public void exitBtnClicked(ActionEvent event) throws SQLException
    {
        Event.fireEvent(Main.getPrimaryStage(),
                new WindowEvent(Main.getPrimaryStage(), WindowEvent.WINDOW_CLOSE_REQUEST ));
    }
}
